package magicloan.auctionpocket.ui.activity.myauction;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

import magicloan.auctionpocket.R;
import magicloan.auctionpocket.adapter.VpFragmentPagerAdapter;
import magicloan.auctionpocket.ui.base.BaseActivity;
import magicloan.auctionpocket.view.indicater.ViewPagerIndicator;

/**
 * Created by hechao on 2018/2/1.
 */
public class MyAuctionActivity extends BaseActivity {

    private ViewPager mViewPager;
    private ViewPagerIndicator mIndicator;//导航栏指示器
    private ArrayList<Fragment> mContents;//装载fragment的集合
    private VpFragmentPagerAdapter mAdapter;
    private TextView mynotice,my_collect,my_page,my_reply,my_wait_show;
    //定义两个颜色和大小
    private int colorA = Color.parseColor("#242424");//选中的颜色
    private int colorB = Color.parseColor("#898989");//默认的颜色
    private float sizeA = 15f;//选中大小
    private float sizeB = 13f;//默认大小
    private int mStartPage ;

    @Override
    protected int setContentView() {
        return R.layout.ac_my_auction;
    }

    @Override
    protected void beforeInitView() {
        Bundle bundle = getIntent().getExtras();
        if ( bundle!=null ){
            mStartPage = bundle.getInt("page");
        }
    }

    @Override
    protected void afterInitView() {

        setTitle("我的竞拍",null);

        mIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        //3个标题
        mynotice = (TextView) findViewById(R.id.mynotice);
        my_page = (TextView) findViewById(R.id.my_page);
        my_reply = (TextView) findViewById(R.id.my_reply);
        my_collect = (TextView) findViewById(R.id.my_collect);
        my_wait_show = (TextView) findViewById(R.id.my_wait_show);

        mynotice.setOnClickListener(this);
        my_page.setOnClickListener(this);
        my_reply.setOnClickListener(this);
        my_collect.setOnClickListener(this);
        my_wait_show.setOnClickListener(this);


        MyAllAuctionFragment fragmentA = new MyAllAuctionFragment();
        MyAuctionningFragment fragmentB = new MyAuctionningFragment();
        MyAllAuctionFragment fragmentC = new MyAllAuctionFragment();
        MyAllAuctionFragment fragmentD = new MyAllAuctionFragment();
        MyAllAuctionFragment fragmentE = new MyAllAuctionFragment();

        mContents = new ArrayList<>();
        mContents.add(fragmentA);
        mContents.add(fragmentB);
        mContents.add(fragmentC);
        mContents.add(fragmentD);
        mContents.add(fragmentE);
        mAdapter = new VpFragmentPagerAdapter(getSupportFragmentManager(), mContents);
        mViewPager.setAdapter(mAdapter);

        setStartPage();

        setListener();

    }

    private void setStartPage(){

        switch (mStartPage){
            case 0:
                setTextAttribute(colorA,colorB,colorB,colorB,colorB,sizeA,sizeB,sizeB,sizeB,sizeB);
                mViewPager.setCurrentItem(0);
                break;
            case 1:
                setTextAttribute(colorB,colorA,colorB,colorB,colorB,sizeB,sizeA,sizeB,sizeB,sizeB);
                mViewPager.setCurrentItem(1);
                break;
            case 2:
                setTextAttribute(colorB,colorB,colorA,colorB,colorB,sizeB,sizeB,sizeA,sizeB,sizeB);
                mViewPager.setCurrentItem(2);
                break;
            case 3:
                setTextAttribute(colorB,colorB,colorB,colorA,colorB,sizeB,sizeB,sizeB,sizeA,sizeB);
                mViewPager.setCurrentItem(3);
                break;
            case 4:
                setTextAttribute(colorB,colorB,colorB,colorB,colorA,sizeB,sizeB,sizeB,sizeB,sizeA);
                mViewPager.setCurrentItem(4);
                break;
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    /**
     * @param v
     */
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.img_public_left:
                finish();
                break;
            case R.id.mynotice:
                setTextAttribute(colorA,colorB,colorB,colorB,colorB,sizeA,sizeB,sizeB,sizeB,sizeB);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.my_page:
                setTextAttribute(colorB,colorA,colorB,colorB,colorB,sizeB,sizeA,sizeB,sizeB,sizeB);
                mViewPager.setCurrentItem(1);
                break;
            case R.id.my_reply:
                setTextAttribute(colorB,colorB,colorA,colorB,colorB,sizeB,sizeB,sizeA,sizeB,sizeB);
                mViewPager.setCurrentItem(2);
                break;
            case R.id.my_collect:
                setTextAttribute(colorB,colorB,colorB,colorA,colorB,sizeB,sizeB,sizeB,sizeA,sizeB);
                mViewPager.setCurrentItem(3);
                break;
            case R.id.my_wait_show:
                setTextAttribute(colorB,colorB,colorB,colorB,colorA,sizeB,sizeB,sizeB,sizeB,sizeA);
                mViewPager.setCurrentItem(4);
                break;
        }
    }

    /**
     * 自己定义一个给textView设置属性的方法
     */
    private void setTextAttribute(int color1, int color2, int color3,int color4, int color5,  float size1, float size2, float size3, float size4, float size5){

        mynotice.setTextColor(color1);
        my_page.setTextColor(color2);
        my_reply.setTextColor(color3);
        my_collect.setTextColor(color4);
        my_wait_show.setTextColor(color5);

        mynotice.setTextSize(size1);
        my_page.setTextSize(size2);
        my_reply.setTextSize(size3);
        my_collect.setTextSize(size4);
        my_wait_show.setTextSize(size5);

    }

    private void setListener() {
        //ViewPager的监听事件
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //让indicator和ViewPager联动
                mIndicator.scroll(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setTextAttribute(colorA,colorB,colorB,colorB,colorB,sizeA,sizeB,sizeB,sizeB,sizeB);
                        break;
                    case 1:
                        setTextAttribute(colorB,colorA,colorB,colorB,colorB,sizeB,sizeA,sizeB,sizeB,sizeB);
                        break;
                    case 2:
                        setTextAttribute(colorB,colorB,colorA,colorB,colorB,sizeB,sizeB,sizeA,sizeB,sizeB);
                        break;
                    case 3:
                        setTextAttribute(colorB,colorB,colorB,colorA,colorB,sizeB,sizeB,sizeB,sizeA,sizeB);
                        break;
                    case 4:
                        setTextAttribute(colorB,colorB,colorB,colorB,colorA,sizeB,sizeB,sizeB,sizeB,sizeA);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

}
